package com.cocostudio.erpapp;

import android.app.ProgressDialog;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.JavascriptInterface;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public abstract class CustomFragmentActivity extends FragmentActivity implements OnClickListener {
    private ImageView leftButton;
    private ImageView rightButton;
    private TextView title;
    private View titleBar;
    private boolean titleBarVisible = true;
    public ProgressDialog pdg;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // not call super.onSaveInstanceState(), bug in support v4:
        // java.lang.IllegalStateException: Can not perform this action after
        // onSaveInstance
    }

    @Override
    public void setContentView(int layoutResID) {
        if (titleBarVisible) {
            View content = getLayoutInflater().inflate(layoutResID, null);
            super.setContentView(attachTitleBar(content, null));
        } else {
            super.setContentView(layoutResID);
        }
    }

    @Override
    public void setContentView(View view) {
        if (titleBarVisible) {
            super.setContentView(attachTitleBar(view, null));
        } else {
            super.setContentView(view);
        }
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        if (titleBarVisible) {
            super.setContentView(attachTitleBar(view, params));
        } else {
            super.setContentView(view, params);
        }
    }

    private View attachTitleBar(View contentView, LayoutParams params) {
        titleBar = getLayoutInflater().inflate(R.layout.titlebar_custom, null);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(titleBar, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        if (params == null) {
            layout.addView(contentView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        } else {
            layout.addView(contentView, params);
        }
        title = (TextView) layout.findViewById(R.id.title);
        leftButton = (ImageView) layout.findViewById(R.id.btn_left);
        leftButton.setOnClickListener(this);
        rightButton = (ImageView) layout.findViewById(R.id.btn_right);
        return layout;
    }

    @Override
    public void onClick(View v) {
        if (!onLeftButtonClicked()) {
            finish();
        }
    }

    protected boolean onLeftButtonClicked() {
        return false;
    }

    protected void setTitleBarVisible(boolean visible) {
        titleBarVisible = visible;
        if (titleBar != null) {
            titleBar.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
    }

    protected int getTitleBarHeight() {
        if (titleBar != null) {
            return titleBar.getHeight();
        }
        return 0;
    }

    protected void setLeftButtonVisible(boolean visible) {
        if (leftButton != null) {
            leftButton.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
    }

    protected void setLeftButtonDrawable(int drawableId) {
        if (leftButton != null) {
            leftButton.setImageResource(drawableId);
        }
    }

    protected void setRightButtonVisible(boolean visible) {
        if (rightButton != null) {
            rightButton.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
        }
    }

    protected void setRightButtonDrawable(int drawableId) {
        if (rightButton != null) {
            setRightButtonVisible(true);
            rightButton.setBackgroundResource(drawableId);
        }
    }

    protected void setRightButtonEnable(boolean enable) {
        if (rightButton != null) {
            rightButton.setEnabled(enable);
        }
    }

    protected void setRightButtonListener(OnClickListener click) {
        if (rightButton != null) {
            setRightButtonVisible(true);
            rightButton.setOnClickListener(click);
        }
    }

    /**
     * 设置标题。
     */
    @Override
    public void setTitle(int resId) {
        if (title != null) {
            title.setText(resId);
        }
    }

    @Override
    public void setTitle(CharSequence string) {
        if (title != null) {
            title.setText(string);
        }
    }

    protected TabSpec newTabSpec(TabHost tabHost, String tag, String title, boolean showLeftDivider, boolean showRightDivider) {
        View view = getLayoutInflater().inflate(R.layout.tab_indicator, null);
        view.findViewById(R.id.left_divider).setVisibility(showLeftDivider ? View.VISIBLE : View.GONE);
        view.findViewById(R.id.right_divider).setVisibility(showRightDivider ? View.VISIBLE : View.GONE);
        ((TextView) view.findViewById(R.id.title)).setText(title);
        return tabHost.newTabSpec(tag).setIndicator(view);
    }
    
    @JavascriptInterface
    public void hidepdg(){
        if (pdg!=null)
            pdg.dismiss();
    }
}
